package lzyc.pointsManagement.business.news.service.impl;

import com.alibaba.fastjson.JSONArray;
import lzyc.pointsManagement.business.attachment.domain.Attachments;
import lzyc.pointsManagement.business.attachment.dto.AttachmentsDto;
import lzyc.pointsManagement.business.news.domain.NewsInfo;
import lzyc.pointsManagement.business.news.mapper.NewsInfoMapper;
import lzyc.pointsManagement.business.news.service.NewsInfoService;
import lzyc.pointsManagement.business.service.AttachmentsService;
import lzyc.pointsManagement.common.core.text.Convert;
import lzyc.pointsManagement.common.utils.DateUtils;
import lzyc.pointsManagement.common.utils.StringUtils;
import lzyc.pointsManagement.common.utils.uuid.IdUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * (NewsInfo)表服务实现类
 *
 * @author yangyang
 * @since 2024-03-10 17:10:19
 */
@Service("newsInfoService")
public class NewsInfoServiceImpl implements NewsInfoService {
    @Resource
    private NewsInfoMapper newsInfoMapper;

    @Resource
    private AttachmentsService attachmentsService;

    /**
     * 通过ID查询单条数据
     *
     * @param newsid 主键
     * @return 实例对象
     */
    @Override
    public NewsInfo queryById(String newsid) {
        NewsInfo newsInfo = this.newsInfoMapper.queryById(newsid);
        if (!StringUtils.isEmpty(newsInfo.getImage())){
            newsInfo.setAttachmentsDetail(Arrays.asList(attachmentsService.getAttachmentsDtoById(newsInfo.getImage())));
        }
        return newsInfo;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<NewsInfo> queryAllByLimit(int offset, int limit) {
        List<NewsInfo> newsInfoList = this.newsInfoMapper.queryAllByLimit(offset, limit);
        for (NewsInfo newsInfo : newsInfoList) {
            if (StringUtils.isNotEmpty(newsInfo.getImage())){
                newsInfo.setAttachmentsDetail(Arrays.asList(attachmentsService.getAttachmentsDtoById(newsInfo.getImage())));
            }
        }
        return newsInfoList;
    }

    /**
     * 根据条件分页查询新闻动态数据
     * 
     * @param info
     * @return
     */
    @Override
    public List<NewsInfo> queryAll(NewsInfo info) {
        List<NewsInfo> newsInfoList = this.newsInfoMapper.queryAll(info);
        for (NewsInfo newsInfo : newsInfoList) {
            if (StringUtils.isNotEmpty(newsInfo.getImage())){
                newsInfo.setAttachmentsDetail(Arrays.asList(attachmentsService.getAttachmentsDtoById(newsInfo.getImage())));
            }
        }
        return newsInfoList;
    }

    /**
     * 新增数据
     *
     * @param newsInfo 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(NewsInfo newsInfo) {
        if (StringUtils.isEmpty(newsInfo.getNewsid())) {
            newsInfo.setNewsid(IdUtils.randomUUID());
        }
        if (StringUtils.isNull(newsInfo.getStarttime())){
            newsInfo.setStarttime(DateUtils.getNowDate());
        }
        newsInfo.setEndtime(DateUtils.getNowDate());
        return this.newsInfoMapper.insert(newsInfo);
    }

    /**
     * 修改数据
     *
     * @param newsInfo 实例对象
     * @return 实例对象
     */
    @Override
    public int update(NewsInfo newsInfo) {
        newsInfo.setEndtime(DateUtils.getNowDate());
        return this.newsInfoMapper.update(newsInfo);
    }

    /**
     * 通过主键删除数据
     *
     * @param ids 主键
     * @return 是否成功
     */
    @Override
    public int deleteById(String ids) {
        String[] newsIds = Convert.toStrArray(ids);
        return this.newsInfoMapper.deleteById(newsIds);
    }

    /**
     * 获取最新新闻动态
     * 
     * @param count 条数
     * @return
     */
    @Override
    public List<NewsInfo> getNewsRecent(Integer count) {
        return queryAllByLimit(0, count);
    }
}